package com.itcast.springboot.waste_recovery.controller;

import com.itcast.springboot.request.RegisterReq;
import com.itcast.springboot.response.StaffMineRes;
import com.itcast.springboot.waste_recovery.common.*;
import com.itcast.springboot.waste_recovery.domain.User;
import com.itcast.springboot.waste_recovery.domain.WasteClassification;
import com.itcast.springboot.waste_recovery.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小破烂
 * @since 2020-08-17
 */
@RestController
@RequestMapping("/waste_recovery/user")
@Slf4j
@Api(tags = "用户类接口")
public class UserController {
    @Resource
    UserService userService;

    @PostMapping(value = "/customer/register/{role_id}")
    @ApiOperation(value = "用户注册")
    //对方法中参数进行设置
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_type", value = "用户类型（1：用户；2：员工）")
    })
    public CommonResult register(@PathVariable(value = "role_id") Integer roleId,
                                 @RequestBody RegisterReq register){
        //返回登录状态，登录成功取得sessionId
        Integer result = userService.register(register, roleId);
        if (result <= 0){
            return CommonResult.CommonFail();
        }
        return CommonResult.CommonSuccess();
    }

    /**
     * 1.传统登录
     *      前端发送登录请求 =》 接口部分获取用户名和密码 =》 程序员在接口部分手动控制
     * 2.shiro登录
     *      前端发送登录请求 =》 接口部分获取用户名和密码 =》 通过subject.login => realm域的认证方法
     */
    @PostMapping(value = "/customer/login")
    @ApiOperation(value = "登录")
    //对方法中参数进行设置
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "手机号or邮箱"),
            @ApiImplicitParam(name = "password", value = "密码")
    })
    public CommonResult login(String account, String password){
        //返回登录状态，登录成功取得sessionId
        Map<String, Object> map = userService.login(account, password);
        if (map == null){
            return CommonResult.CommonLoginFail();
        }
        return new CommonResult<>(CommonResultCode.CommonSuccess, map);
    }

    @GetMapping("/customer/user_home")
    @ApiOperation(value = "首页")
    @RequiresPermissions("user-operated")       //拥有 user-operated 才可以访问
    public WasteClassification getWasteData(){
        return null;
    }

    @GetMapping("/customer/user_mine/{id}")
    @ApiOperation(value = "我的")
    //对方法中参数进行设置
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id")
    })
    @RequiresPermissions("user-operated")       //拥有 user-operated 才可以访问
    public CommonResult getUserMine(@PathVariable Integer id){
        Map<String, Object> userMine = userService.getUserMine(id);
        CommonResult<Map<String, Object>> commonResult = new CommonResult<>(CommonResultCode.CommonSuccess, userMine);
        return commonResult;
    }

    @GetMapping("/customer/personal_information/{id}")
    @ApiOperation(value = "设置：个人信息展示")
    //对方法中参数进行设置
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id")
    })
    @RequiresPermissions("user-operated")       //拥有 user-operated 才可以访问
    public CommonResult getPersonalInformation(@PathVariable Integer id){
        Map<String, Object> perInfo = userService.getPersonalInformation(id);
        return new CommonResult<>(CommonResultCode.CommonSuccess, perInfo);
    }

    @PutMapping("/customer/username_or_email")
    @ApiOperation(value = "改名字或邮箱")
    //对方法中参数进行设置
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id"),
            @ApiImplicitParam(name = "username", value = "用户名"),
            @ApiImplicitParam(name = "user_email", value = "邮箱"),
    })
    @RequiresPermissions("user-operated")       //拥有 user-operated 才可以访问
    public CommonResult updateUsernameOrEmail(@RequestParam Integer id,
                                              @RequestParam(required = false) String username,
                                              @RequestParam(value = "user_email", required = false) String userEmail){
        Integer result = userService.updateUsername(id, username, userEmail);
        if (result < 0){
            return CommonResult.CommonFail();
        }
        return CommonResult.CommonSuccess();
    }

    @PostMapping("/customer/bind_email/{user_email}/{subject}/{text}")
    @ApiOperation(value = "绑定邮箱：发送验证信息")
    //对方法中参数进行设置
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_email", value = "接收者"),
            @ApiImplicitParam(name = "subject", value = "邮件标题"),
            @ApiImplicitParam(name = "text", value = "邮件内容"),
    })
    @RequiresPermissions("user-operated")       //拥有 user-operated 才可以访问
    public CommonResult bindEmail(@PathVariable(value = "user_email") String userEmail,
                                  @PathVariable String subject,
                                  @PathVariable String text){
        Boolean result = userService.bindEmail(userEmail, subject, text);
        if (!result){
            return CommonResult.CommonFail();
        }
        return CommonResult.CommonSuccess();
    }

    @GetMapping("/customer/real_name_auth/{user_id}/{user_real_name}/{user_num}")
    @ApiOperation(value = "实名认证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_id", value = "用户id"),
            @ApiImplicitParam(name = "user_real_name", value = "用户真实姓名"),
            @ApiImplicitParam(name = "user_num", value = "身份证号"),
    })
    @RequiresPermissions("user-operated")       //拥有 user-operated 才可以访问
    public CommonResult realNameAuth(@PathVariable(value = "user_id") Integer userId,
                                    @PathVariable(value = "user_real_name") String userRealName,
                                     @PathVariable(value = "user_num") String userNum){
        Integer result = userService.realNameAuth(userId, userRealName, userNum);
        if (result == 0) {
            return CommonResult.CommonAuthed();
        }
        if (result < 0){
            return CommonResult.CommonFail();
        }
        return CommonResult.CommonSuccess();
    }

    @GetMapping("staff/staff_mine/{user_id}")
    @ApiOperation(value = "员工端：我的")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_id", value = "用户id")
    })
    @RequiresPermissions("staff-operated")       //拥有 user-operated 才可以访问
    public CommonResult getStaffMine(@PathVariable(value = "user_id") Integer userId){
        StaffMineRes staffMine = userService.getStaffMine(userId);
        return new CommonResult(CommonResultCode.CommonSuccess, staffMine);
    }

    @PostMapping("staff/userurl/{user_id}")
    @ApiOperation(value = "员工端：修改头像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_id", value = "用户id"),
            @ApiImplicitParam(name = "img", value = "图片")
    })
    @RequiresPermissions("staff-operated")       //拥有 user-operated 才可以访问
    public CommonResult updateStaffUrl(@PathVariable(value = "user_id") Integer userId, MultipartFile img){
        String imagePath = userService.updateStaffUrl(userId, img);
        if (imagePath.equals("-1")){
            return CommonResult.CommonFail();
        }
        return new CommonResult(CommonResultCode.CommonSuccess, imagePath);
    }

    @GetMapping("autherror")
    @ApiOperation(value = "未登录跳转的页面")
    public CommonResult autherror(){
        return CommonResult.CommonUnlogin();
    }
}

